package com.gxa.admin.recycling.service.impl;

import com.gxa.admin.recycling.mapper.AdminMapper;
import com.gxa.admin.recycling.service.AdminService;
import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.Admin;
import com.gxa.core.recycling.pojo.Menu;
import com.gxa.core.recycling.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author R
 * @data 2021-12-03 11:21
 */
@Service
public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {

    @Autowired
    AdminMapper mapper;

    @Override
    public List<Map<String, Object>> getAllByPage(String nickName, String name, String from, String to, Integer page, Integer size) {
        List<Map<String, Object>> allByPage = mapper.getAllByPage(nickName, name, from, to, (page - 1) * size, size);
        for (int i = 0; i < allByPage.size(); i++) {
            String registerTime = allByPage.get(i).get("registerTime").toString();
            allByPage.get(i).put("registerTime",registerTime);
        }
        return allByPage;
    }

    @Override
    public Integer getAllByPageCount(String nickName, String name, String from, String to, Integer page, Integer size) {
        page = null;
        size = null;
        List<Map<String, Object>> allByPage = mapper.getAllByPage(nickName, name, from, to, page, size);
        return allByPage.size();
    }

    @Override
    public Integer getRoleById() {
        Integer roleById = mapper.getRoleById();
        return roleById;
    }

    @Override
    public String getRoleNameById(Integer id) {
        String roleNameById = mapper.getRoleNameById(id);
        return roleNameById;
    }



    @Override
    public Admin loginByPwd(String nickName, String pwd) throws BusinessException {
        Admin admin = mapper.loginByPwd(nickName, pwd);
        if (admin == null){
            throw new BusinessException(ExceptionCode.PWD_WRONG);
        }
        return admin;
    }
}
